package com.zhuj.core;

import io.netty.channel.Channel;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2018/1/3.
 */
public class Global {

    private static ConcurrentMap<String,Channel> clientChannel = new ConcurrentHashMap();

    private static AtomicInteger request_id = new AtomicInteger();

    public static String NRPC_REG = "nrpc_register_service";

    public static void setClientChannel(String key,Channel channel){
        clientChannel.put(key,channel);
    }

    public static Channel getClientChannel(String key){
        return clientChannel.get(key);
    }

    public enum BasicType{
        BYTE("byte",byte.class),SHORT("short",short.class),INT("int",int.class),LONG("long",long.class),
        CHAR("char",char.class),FLOAT("float",float.class),DOUBLE("double",double.class),BOOLEAN("boolean",boolean.class);

        private String name;

        private Class clazz;

        BasicType(String s, Class clazz) {
            this.name = s;
            this.clazz = clazz;
        }

        public static Class getType(String name){
            for(BasicType b:BasicType.values()){
                if (b.getName().equals(name))
                    return b.getClazz();
            }
            return null;
        }

        public String getName() {
            return name;
        }

        public Class getClazz() {
            return clazz;
        }
    }

    public static int getRequest_id() {
        if (request_id.get() < Integer.MAX_VALUE){
            return request_id.getAndIncrement();
        }else {
            request_id = new AtomicInteger();
            return request_id.get();
        }
    }
}
